<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-18
 * Time: 下午4:57
 */

class NewsController extends Controller
{
    private $_title;
    private $_keywords;
    private $_description;

    public function init()
    {
        parent::init();
        // 取得用户信息
        $user_info = $this->_session->offsetGet ( 'loginInfos' );
        $this->_view->userInfo=$user_info;
    }
    public function indexAction(){
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $this->_title = '新闻首页';
        $this->_keywords = '新闻首页';
        $this->_description = '新闻首页';

        //分类
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $News=new NewsModel();
        //这边查询数据
        //sort_order=1
        $sort1 = $News->getCateInfo("is_show='Y' and sort_order='1'");
        $sort1_detail = $News->getCateInfoDetail("cate_id='{$sort1['id']}' and is_release='Y'",$limit=8,$order="create_time desc");
        if(!empty($sort1_detail)){
            foreach($sort1_detail as $k=>$v){
                $sort1_detail[$k]['title'] = Tools::substr($v['title'],0,12,"utf-8")."...";
                $sort1_detail[$k]['release_time'] = Tools::substr($v['release_time'],0,10,"utf-8");
            }
        }
        $sort2 = $News->getCateInfo("is_show='Y' and sort_order='2'");
        $sort2_detail = $News->getCateInfoDetail("cate_id='{$sort2['id']}' and is_release='Y'",$limit=6,$order="create_time desc");
        if(!empty($sort2_detail)){
            foreach($sort2_detail as $k=>$v){
                $sort2_detail[$k]['title'] = Tools::substr($v['title'],0,12,"utf-8")."...";
                $sort2_detail[$k]['release_time'] = Tools::substr($v['release_time'],0,10,"utf-8");
            }
        }
        $sort3 = $News->getCateInfo("is_show='Y' and sort_order='3'");
        $sort3_detail = $News->getCateInfoDetail("cate_id='{$sort3['id']}' and is_release='Y'",$limit=6,$order="create_time desc");
        if(!empty($sort3_detail)){
            foreach($sort3_detail as $k=>$v){
                $sort3_detail[$k]['title'] = Tools::substr($v['title'],0,12,"utf-8")."...";
                $sort3_detail[$k]['release_time'] = Tools::substr($v['release_time'],0,10,"utf-8");
            }
        }
        $sort4 = $News->getCateInfo("is_show='Y' and sort_order='4'");
        $sort4_detail = $News->getCateInfoDetail("cate_id='{$sort4['id']}' and is_release='Y'",$limit=6,$order="create_time desc");
        if(!empty($sort4_detail)){
            foreach($sort4_detail as $k=>$v){
                $sort4_detail[$k]['title'] = Tools::substr($v['title'],0,12,"utf-8")."...";
                $sort4_detail[$k]['release_time'] = Tools::substr($v['release_time'],0,10,"utf-8");
            }
        }
        $sort5 = $News->getCateInfo("is_show='Y' and sort_order='5'");
        $sort5_detail = $News->getCateInfoDetail("cate_id='{$sort5['id']}' and is_release='Y'",$limit=6,$order="create_time desc");
        if(!empty($sort5_detail)){
            foreach($sort5_detail as $k=>$v){
                $sort5_detail[$k]['title'] = Tools::substr($v['title'],0,12,"utf-8")."...";
                $sort5_detail[$k]['release_time'] = Tools::substr($v['release_time'],0,10,"utf-8");
            }
        }
        $sort6 = $News->getCateInfo("is_show='Y' and sort_order='6'");
        $sort6_detail = $News->getCateInfoDetail("cate_id='{$sort6['id']}' and is_release='Y'",$limit=3,$order="create_time desc");
        if(!empty($sort6_detail)){
            foreach($sort6_detail as $k=>$v){
                $sort6_detail[$k]['title'] = Tools::substr($v['title'],0,12,"utf-8")."...";
                $sort6_detail[$k]['release_time'] = Tools::substr($v['release_time'],0,10,"utf-8");
            }
        }
        $sort7 = $News->getCateInfo("is_show='Y' and sort_order='7'");
        $sort7_detail = $News->getCateInfoDetail("cate_id='{$sort7['id']}' and is_release='Y'",$limit=3,$order="create_time desc");
        if(!empty($sort7_detail)){
            foreach($sort7_detail as $k=>$v){
                $sort7_detail[$k]['title'] = Tools::substr($v['title'],0,12,"utf-8")."...";
                $sort7_detail[$k]['release_time'] = Tools::substr($v['release_time'],0,10,"utf-8");
            }
        }

        $this->_view->title = $this->_title;
        $this->_view->keywords  = $this->_keywords;
        $this->_view->description = $this->_description;
        $this->_view->cate=$cate;
        $this->_view->kd=$kd;
        $this->_view->image_url = IMAGE_URL_ADMIN;

        $this->_view->sort1=$sort1;
        $this->_view->sort1_detail=$sort1_detail;
        $this->_view->sort2=$sort2;
        $this->_view->sort2_detail=$sort2_detail;
        $this->_view->sort3=$sort3;
        $this->_view->sort3_detail=$sort3_detail;
        $this->_view->sort4=$sort4;
        $this->_view->sort4_detail=$sort4_detail;
        $this->_view->sort5=$sort5;
        $this->_view->sort5_detail=$sort5_detail;
        $this->_view->sort6=$sort6;
        $this->_view->sort6_detail=$sort6_detail;
        $this->_view->sort7=$sort7;
        $this->_view->sort7_detail=$sort7_detail;

    }

    public function listAction(){
        global $_getAreaNames;//头部信息
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $id =(int) $this->_request->getParam("pid");
        $this->_title = '新闻列表';
        $this->_keywords = '新闻列表';
        $this->_description = '新闻列表';
        $where = "1=1";
        $url="/news/list";
        //获取分页
        $page = $this->_request->getParam("page",1);
        $itempage = $this->_request->getParam("itempage",0);
        if(!empty($itempage) && $itempage>10){
            define('ITEM_PER_PAGE',$itempage);
            $url .="/itempage/".$itempage;
        }else{
            define('ITEM_PER_PAGE',10);
        }
        if(!empty($id)){
            $where="cate_id={$id}";
        }
        $News=new NewsModel();
        $totalItems =$News->getNewsCount($where);

        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil($totalItems / ITEM_PER_PAGE);
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }
        $field= array(
            'summary',
            'release_time',
            'id',
            'image',
            'title'
        );
        $order = "update_time desc";
        $newlist=$News->NewsList($offset,ITEM_PER_PAGE,$field,$where,$order);
        if(!empty($id)){
            $url .="/pid/$id/page/%d";
        }
        else{
            $url .= "/page/%d";
        }

        $links = Pagination::createPage($url, ITEM_PER_PAGE, $page, $totalItems, 2 ,'_self',true);
        //分类
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        //原油指数
        $IndexMark = $index->getIndexMark("IndexMark");
        //热门文章
        $hot=$News->Order();
        //文章分类
        $newstypes= new NewscateModel();
        $newstype=$newstypes->NewsTpye();
        $this->_view->newstype=$newstype;
        $this->_view->hot = $hot;
        $this->_view->page = $page;
        $this->_view->links =$links;
        $this->_view->newlist = $newlist;
        $this->_view->newscount=$totalItems;
        $this->_view->title = $this->_title;
        $this->_view->keywords  = $this->_keywords;
        $this->_view->description = $this->_description;
        $this->_view->_getAreaNames = $_getAreaNames;
        $this->_view->cate=$cate;
        $this->_view->IndexMark=$IndexMark;
        $this->_view->kd=$kd;
        $this->_view->image_url = IMAGE_URL_ADMIN;
    }

    public function newsdetailAction(){
        global $_getAreaNames;//头部信息
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $id =(int) $this->_request->getParam("id",1);

        $feild=array(
            'id',
            'title',
            'release_time',
            'image',
            'content',
            'keywords',
            'description'
        );
        $new=new NewsModel();
        //热门文章
        $hot=$new->Order();
        $this->_view->hot=$hot;
        $NewsList=$new->NewsListById($id,$feild);
        //上一篇，下一篇文章
        $up=$new->Up($id);//上一篇文章
        $down=$new->Down($id);//下一篇文章
        //浏览次数
        $feilds=array(
            'click_num',
        );

        $num=$new->NewsListById($id,$feilds);
        if(empty($num['click_num'])){
            $num['click_num']="0";
        }
        $click_num=array('click_num'=>$num['click_num']+1);
        $new->Browse($id,$click_num);
        //分类
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        //原油指数
        $IndexMark = $index->getIndexMark("IndexMark");
        //文章分类
        $newstypes= new NewscateModel();
        $newstype=$newstypes->NewsTpye();
        //相似文章
        $oldtitle=$new->OldTitle($id);
        $cateid=$oldtitle['cate_id'];
        $where="cate_id={$cateid} and id<>{$id}";
        $feildss=array(
            'id',
            'title'
        );
        $newtitle=$new->getTitleByWhere($where,$feildss);
        $this->_title = $NewsList['title'].'_塑料交易网';
        $this->_keywords = $NewsList['keywords'];
        $this->_description = $NewsList['description'];

//        var_dump($newtitle);exit;
        $this->_view->kd=$kd;
        $this->_view->newtitle=$newtitle;
        $this->_view->newstype=$newstype;
        $this->_view->NewsList=$NewsList;
        $this->_view->title = $this->_title;
        $this->_view->keywords  = $this->_keywords;
        $this->_view->description = $this->_description;
        $this->_view->cate=$cate;
        $this->_view->IndexMark=$IndexMark;
        $this->_view->up=$up;
        $this->_view->down=$down;
        $this->_view->_getAreaNames = $_getAreaNames;
    }

}